package com.hundsun.aitest.model.testanalysistree;

import java.util.ArrayList;
import java.util.List;

public class Node {
    private String name;
    private List<Node> children;
    private Node parent;

    public Node(String name) {
        this.name = name;
        this.children = new ArrayList<>();
    }

    public void addChild(Node child) {
        children.add(child);
        child.setParent(this);
    }

    public void setParent(Node parent) {
        this.parent = parent;
    }

    public String getName() {
        return name;
    }

    public List<Node> getChildren() {
        return children;
    }

    public List<LeafNode> getLeafNodes() {
        List<LeafNode> leafNodes = new ArrayList<>();
        if (children.isEmpty()) {
            // 如果是叶子节点，构建完整的路径，包括根节点
            leafNodes.add(new LeafNode(buildFullPath(this), this.name));
        } else {
            for (Node child : children) {
                leafNodes.addAll(child.getLeafNodes());
            }
        }
        return leafNodes;
    }

    private String buildFullPath(Node node) {
        if (node.getParent() == null) {
            return node.getName(); // 根节点直接返回自己的名字
        } else {
            return buildFullPath(node.getParent()) + "/" + node.getName();
        }
    }



    public Node getParent() {
        return parent;
    }
}